<?php 

/**
 * CoreController
 * @author lk
 */
class BibleController extends My_Controller {

	/** Biblia */
	public function bibleformAction(){
		echo $this->_helper->form(new Bible());
	}
	
	public function biblegridAction(){
		$this->_helper->grid(new Bible());
	}
	
	public function bibledelAction(){
		echo $this->_helper->delete(new Bible());
	}
	
	/** Księga */
	public function bookformAction(){
		echo $this->_helper->form(new Book());
	}
	
	public function bookgridAction(){
		$this->_helper->grid(new Book());
	}
	
	public function bookdelAction(){
		echo $this->_helper->delete(new Book());
	}
	
	/** Rozdział */
	public function chapterformAction(){
		echo $this->_helper->form(new Chapter());
	}
	
	public function chaptergridAction(){
		$this->_helper->grid(new Chapter());
	}
	
	public function chapterdelAction(){
		echo $this->_helper->delete(new Chapter());
	}
	
	
	/** Księgi Biblii */
	public function biblebookformAction(){
		echo $this->_helper->form(new BibleBook());
	}
	
	public function biblebookgridAction(){
		$this->_helper->grid(new BibleBook());
	}
	
	public function biblebookdelAction(){
		echo $this->_helper->delete(new BibleBook());
	}
	
	/** Wersy Biblii */
	public function bibleverseformAction(){
		echo $this->_helper->form(new BibleVerse());
	}
	
	public function bibleversesformAction(){
		
		$r = $this->getRequest();
		$d = $r->getParam('delimiter');// : /\{(.){*}\}/
		
		/*
		
		/Mk 2,(.){2}/
		
		*/
		
		$n = $r->getParam('verse_number_start');
		$b = $r->getParam('book_id');
		$c = $r->getParam('chapter_id');
		$B = $r->getParam('bible_id');
		
		$verse = new Verse();
		$bibleverse = new BibleVerse();
		$map = array(); // mapa numerów wersów
		
		$q = $verse->_q(array(
			'where'=>Utils::quote('book_id = ? AND chapter_id = ? AND verse_number >= ?',array($b,$c,$n))
		));
		
		while($row = $q->fetchObject()){
			$map[$row->verse_number] = $row->verse_id;
		}
		
		$all = preg_split($d, $r->getParam('bible_verse_text'));
		
		for($i = 0;$i<count($all);$i++){
			
			if(!array_key_exists($i+$n, $map)){
				$map[$i+$n]= $verse->insert(array(
					'verse_number'=>$i+$n,
					'book_id'=>$b,
					'chapter_id'=>$c
				));
			}
			
			$bibleverse->insert(array(
				'verse_id'=>$map[$i+$n],
				'bible_verse_text'=>$all[$i],
				'bible_id'=>$B
			));
		}
		Validation::sayOk();
	}
	
	public function bibleversegridAction(){
		$this->_helper->grid->_(array(
			'class'=>'BibleVerse',
			'join'=>array('verse','verse.verse_id = #.verse_id',array('*'))
		));
	}
	
	public function bibleversedelAction(){
		echo $this->_helper->delete(new BibleVerse());
	}
	
	/** Wersy  */
	public function verseformAction(){
		$number = Utils::rq('verse_number');
		$verse = new Verse();
		if(strpos($number,'-')!==false){
			if(Utils::rq('verse_id')){
				Validation::react('Edytujesz istniejący vers');
			}
			$ns = explode('-',$number);
			
			for($i=(int)$ns[0]; $i<=(int)$ns[1];$i++){
				Utils::rq('verse_number',$i);
				$number = $this->_helper->form($verse);
			}
			echo $number;// ostatni wynik
			
		} else echo $this->_helper->form($verse);
	}
	
	public function versegridAction(){
		$this->_helper->grid(new Verse());
	}
	
	public function versedelAction(){
		echo $this->_helper->delete(new Verse());
	}
	
	/** Testamenty */
	public function testamentformAction(){
		echo $this->_helper->form(new Testament());
	}
	
	public function testamentgridAction(){
		$this->_helper->grid(new Testament());
	}
	
	public function testamentdelAction(){
		echo $this->_helper->delete(new Testament());
	}
}
